package com.tenbent.product.center.insures.action;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.web.Page;
import com.tenbent.product.center.insures.bo.SkuPrice;
import com.tenbent.product.center.insures.dto.SkuPriceDto;
import com.tenbent.product.center.insures.dto.search.SkuPriceSearchPageDto;
import com.tenbent.product.center.insures.service.SkuPriceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * sku价格控制器
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/8/29.
 */
@RestController
public class SkuPriceController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SkuPriceController.class);

	public static class Url {
		/** 添加保障套餐 */
		public static final String save = "/api/skuPrice/save";
		/** 修改保障套餐 */
		public static final String update = "/api/skuPrice/update";
		/** 删除保障套餐 */
		public static final String delete = "/api/skuPrice/delete/{id}";
		/** 根据产品ID和sku值组合计算保费 */
		public static final String queryPrice = "/api/skuPrice/query_price";
		/** 保障套餐查询 */
		public static final String query = "/api/skuPrice/query";
	}

	@Autowired
	@Qualifier("skuPriceServiceImpl")
	private SkuPriceService skuPriceService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseStatus(HttpStatus.CREATED)
	public SkuPriceDto saveApi(@Valid @RequestBody SkuPriceDto skuPriceDto, Errors errors,
			@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		processErrors(errors);
		settingSaveManager(skuPriceDto, managerId);
		SkuPrice skuPrice = skuPriceDto.transToBo();
		skuPriceService.add(skuPrice);
		return new SkuPriceDto(skuPrice);
	}

	@RequestMapping(value = Url.update, method = {
			RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SkuPriceDto updateApi(@Valid @RequestBody SkuPriceDto skuPriceDto, Errors errors,
			@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		if (skuPriceDto.getId() == null) {
			errors.rejectValue("id", "sku.price.id.not.empty", getMessage("sku.price.id.not.empty", null));
		}
		processErrors(errors);
		settingModiferManager(skuPriceDto, managerId);
		SkuPrice skuPrice = skuPriceDto.transToBo();
		skuPriceService.update(skuPrice);
		return new SkuPriceDto(skuPrice);
	}

	@RequestMapping(value = Url.delete, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void deleteApi(@PathVariable Integer id, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		skuPriceService.deleteLogic(id, managerId);
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<SkuPriceDto> query(SkuPriceSearchPageDto searchPageDto) {

		PageResult<SkuPrice> pageResult = skuPriceService.queryPageObj(searchPageDto.transToPageable());
		return new Page(change.changesToDTOPage(pageResult, SkuPriceDto.class));
	}

	@RequestMapping(value = Url.queryPrice, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<SkuPriceDto> queryPrice(@RequestParam("productId") Integer productId,
			@RequestParam("riskValues") String riskValues) {

		SkuPrice skuPrice = new SkuPrice();
		skuPrice.setProductId(productId);
		skuPrice.setRiskValues(riskValues);
		List<SkuPrice> list = skuPriceService.queryObj(skuPrice);
		return change.changesToDTOList(list, SkuPriceDto.class);
	}

}
